import java.util.HashSet;
import java.util.Set;

/**
 * @program: LeetCode
 * @description: LeetCode : 349. 两个数组的交集
 * @author: WXY
 * @create: 2023-01-14 21:08
 * @Version 1.0
 **/
public class Num349_intersection {
    public static int[] intersection(int[] nums1, int[] nums2) {
        HashSet<Integer> set = new HashSet<>();
        for (int i = 0; i < nums1.length; i++) {
            set.add(nums1[i]);
        }
        Set<Integer> ans = new HashSet<>();
        for (int i = 0; i < nums2.length; i++) {
            if (set.contains(nums2[i])) {
                ans.add(nums2[i]);
            }
        }
        int[] res = new int[ans.size()];
        int index = 0;
        for (int num :
                ans) {
            res[index++] = num;
        }
        return res;
    }


    public static void main(String[] args) {
        int[] arr1 = {1, 2, 2, 3};
        int[] arr2 = {2, 2};
        int[] ans = intersection(arr1, arr2);
        for (int num : ans) {
            System.out.print(num + " ");
        }
    }
}
